﻿using System;
using System.Linq.Expressions;

namespace Spaanjaars.ContactManager.Infrastructure.Domain
{
    public class Specification<T> : ISpecification<T>
    {
        private readonly Expression<Func<T, bool>> _predicate;
        private readonly Func<T, bool> _predicateCompiled;

        public Specification(Expression<Func<T, bool>> predicate)
        {
            _predicate = predicate;
            _predicateCompiled = _predicate.Compile();
        }

        public Expression<Func<T, bool>> Predicate
        {
            get { return _predicate; }
        }

        public bool IsSatisfiedBy(T entity)
        {
            return _predicateCompiled.Invoke(entity);
        }
    }
}